package userInteraction;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

@SuppressWarnings("serial")
public class updateUserInformation implements Serializable {
	
	public Boolean updateInformation(String firstName, String lastName, String phoneNumber, String city, String password,String emailAddress) throws Exception{
		Connection con = null;
		try
		{
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection("jdbc:mysql://localhost:8889/licenseDb","root", "root");
		
			PreparedStatement query = (PreparedStatement) con.prepareStatement("UPDATE Customer join Logins on Customer.customerId=Logins.customerId SET Customer.firstName=?, Customer.lastName=?, Customer.phoneNumber=?, Customer.city=?, Logins.password=? WHERE Customer.emailAddress=?");
			query.setString(1, firstName);
			query.setString(2, lastName);
			query.setString(3, phoneNumber);
			query.setString(4, city);
			query.setString(5, password);
			query.setString(6, emailAddress);
			
			Integer rowCount = query.executeUpdate();
			if(rowCount == 1){
				return true;
			}
			
		}
		catch (Exception e){
			System.out.println(e.getMessage());
		}
		finally{
			try {
				if(con != null)
					con.close();
			} 
			catch(Exception e) {}
		}
		return false;
	}
		
	public Boolean checkAdmin(String emailAddress) throws Exception{
		Connection con = null;
		int flag = 0;
		boolean isExecuted = false;
		try
		{
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection("jdbc:mysql://localhost:8889/licenseDb","root", "root");

			PreparedStatement query = (PreparedStatement) con.prepareStatement("SELECT roleId from Logins where emailAddress = ?");
			query.setString(1, emailAddress);
			ResultSet re = query.executeQuery();
			while(re.next()) 
			{
				flag = re.getInt(1);
				if(flag==2)
				{
					isExecuted = true;
				}
			}
		}
		catch (Exception e){
			System.out.println(e.getMessage());
		}
		finally{
			try {
				if(con != null)
					con.close();
			} 
			catch(Exception e) {}
		}
		return isExecuted;
	}

	public Boolean upgradeToAdmin(String emailAddress, Integer roleId) throws Exception{
		Connection con = null;
		try
		{
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection("jdbc:mysql://localhost:8889/licenseDb","root", "root");
			
			PreparedStatement query = (PreparedStatement) con.prepareStatement("UPDATE Customer join Logins on Customer.customerId=Logins.customerId SET Logins.roleId=? WHERE Customer.emailAddress=?");
			query.setInt(1, roleId);
			query.setString(2, emailAddress);
			
			Integer rowCount = query.executeUpdate();
			if(rowCount == 1){
				return true;
			}			
		}
		catch (Exception e){
			System.out.println(e.getMessage());
		}
		finally{
			try {
				if(con != null)
					con.close();
			} 
			catch(Exception e) {}
		}
		return false;
	}
	
	public static void main(String args[]) throws Exception{
		updateUserInformation x = new updateUserInformation();
		System.out.println(x.checkAdmin("luci_ionutz_88@yahoo.com"));
		
	}
}